//
//  YUYAMessageViewController.m
//  com.yigu.yuya
//
//  Created by hxp on 16/3/2.
//  Copyright © 2016年 hxp. All rights reserved.
//

#import "YAYIMessageViewController.h"
#import "YAYILoginViewController.h"
#import "YAYISystemInfoController.h"

#import "YAYISysMessageCell.h"
#import "YAYIMessageModel.h"


@interface YAYIMessageViewController ()

@property (nonatomic, strong) NSArray *dictArray;
@property (nonatomic, strong) NSMutableArray *modelArray;

@end

@implementation YAYIMessageViewController


- (NSMutableArray *)modelArray
{
    if (nil == _modelArray) {
        _modelArray = [NSMutableArray array];
        
        
        for (NSDictionary *dict in self.dictArray) {
            
            YAYIMessageModel *model = [[YAYIMessageModel alloc] init];
            [model setValuesForKeysWithDictionary:dict];
            
            [_modelArray addObject:model];
        }
    }
    return _modelArray;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"消息";
    
    self.dictArray = @[@{@"typename2": @"系统消息", @"content": @"系统消息", @"iconName": @"systemMessage"},
                  @{@"typename2": @"预约提醒", @"content": @"预约提醒", @"iconName": @"appointRemind"},
                  @{@"typename2": @"雅同精选", @"content": @"雅同精选", @"iconName": @"yayiSelected"}];
    
    self.tableView.separatorStyle = UITableViewCellSelectionStyleNone;
    self.tableView.tableFooterView = [[UIView alloc] init];
    
    [self getNoticeCount];
}


#pragma mark - 获取数据
- (void)getNoticeCount
{
    
    for (int i = 0; i < self.dictArray.count; i++) {
        
        NSDictionary *dict = self.dictArray[i];
        
        YAYIMessageModel *model = self.modelArray[i];
        [model setValuesForKeysWithDictionary:dict];
        
        [_modelArray addObject:model];
    }
    
    [YAYINetWorking getWithURL:noticeCount_url param:nil finish:^(NSData *data, NSDictionary *dict, NSError *error) {
        if (error == nil) {
            YAYILog(@"返回：%@", dict);
            
            
            if (dict == nil || [dict isKindOfClass:[NSNull class]] || ![dict[@"status"] isEqualToString:@"success"]) {
            
                return;
            }else{
                
                NSArray *dictArray = dict[@"data"];
                [dictArray enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                    
                    NSInteger type = [obj[@"type"] integerValue];
                    YAYIMessageModel *model = self.modelArray[type - 1];
                    [model setValuesForKeysWithDictionary:obj];
                }];
                
                [self.tableView reloadData];
            }
        }
        
    }];
}


- (void)clearNoticeByType:(NSInteger)type
{
    
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    param[@"type"] = @(type);
    
    [YAYINetWorking postJsonSerilizeWithURL:clearNoticeCount_url param:param finish:^(NSData *data, NSDictionary *dict, NSError *error, NSDictionary *responseError) {
        
        YAYILog(@"返回：%@", dict);
        if (dict == nil || [dict isKindOfClass:[NSNull class]] || ![dict[@"status"] isEqualToString:@"success"]) {
            
            return;
        }else{
            
            YAYILog(@"清除未读消息成功！");
            YAYIMessageModel *model = self.modelArray[type - 1];
            model.news_count = 0;
            
            NSIndexPath *indexPath = [NSIndexPath indexPathForRow:type - 1 inSection:0];
            [self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
        }
    }];
}


#pragma mark - 点击事件


#pragma mark - 代理方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 3;
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 60;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    YAYIMessageModel *model = self.modelArray[indexPath.row];
    
    YAYISysMessageCell *cell = [YAYISysMessageCell cellWith:tableView];
    cell.model = model;
    
    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    YAYISystemInfoController *msgListVc = [[YAYISystemInfoController alloc] init];
    msgListVc.type = indexPath.row + 1;
    [self.navigationController pushViewController:msgListVc animated:YES];
    
    // 消息未读置零
    [self clearNoticeByType:indexPath.row + 1];
}

@end
